package com.common.app.widget

import android.annotation.SuppressLint
import android.content.Context
import android.content.res.TypedArray
import android.graphics.Canvas
import android.graphics.DashPathEffect
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import android.view.View
import androidx.annotation.RequiresApi
import com.common.app.R

/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：2024/7/12
 *
 * 描述：点虚线
 *点
 * 修订历史：
 *
 */
class DashedView : View {
    private var mPaint: Paint? = null
    private var mDashColor = 0
    private var mDashGap = 0
    private var mDashWidth = 0
    private var mStrokeWidth = 0f
    private val mLinePath = Path()
    private var mDashedType = 0

    constructor(context: Context?) : super(context) {
        initTypedArray(null as TypedArray?)
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        initTypedArray(context.obtainStyledAttributes(attrs, R.styleable.DashedView))
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        initTypedArray(context.obtainStyledAttributes(attrs, R.styleable.DashedView))
    }

    @RequiresApi(api = 21)
    constructor(
        context: Context,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int
    ) : super(context, attrs, defStyleAttr, defStyleRes) {
        initTypedArray(context.obtainStyledAttributes(attrs, R.styleable.DashedView))
    }

    private fun initTypedArray(typedArray: TypedArray?) {
        mPaint = Paint()
        mPaint!!.isAntiAlias = true
        mPaint!!.style = Paint.Style.STROKE
        val d = this.context.resources.displayMetrics.density
        if (typedArray != null) {
            mDashColor = typedArray.getColor(R.styleable.DashedView_dashColor, 0)
            mStrokeWidth =
                typedArray.getDimensionPixelSize(R.styleable.DashedView_strokeWidth, 0).toFloat()
            mDashGap = typedArray.getDimensionPixelSize(R.styleable.DashedView_dashGap, 0)
            mDashWidth = typedArray.getDimensionPixelSize(R.styleable.DashedView_dashWidth, 1)
            mDashedType = typedArray.getInt(R.styleable.DashedView_dashType, 0)
            typedArray.recycle()
        } else {
            mDashColor = -12303292
        }
        if (mStrokeWidth <= 0.0f) {
            mStrokeWidth = d
        }
        if (mDashWidth <= 0) {
            mDashWidth = (2.0f * d).toInt()
        }
        if (mDashGap <= 0) {
            mDashGap = (2.0f * d).toInt()
        }
    }

    @SuppressLint("DrawAllocation")
    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)
        mPaint!!.pathEffect =
            DashPathEffect(floatArrayOf(mDashWidth.toFloat(), mDashGap.toFloat()), 0.0f)
    }

    override fun onDraw(canvas: Canvas) {
        mPaint!!.color = mDashColor
        mPaint!!.strokeWidth = mStrokeWidth
        if (mDashedType == 0) {
            mLinePath.reset()
            mLinePath.moveTo(0.0f, this.height.toFloat() * 0.5f)
            mLinePath.lineTo(this.width.toFloat(), this.height.toFloat() * 0.5f)
            canvas.drawPath(mLinePath, mPaint!!)
        } else if (mDashedType == 1) {
            mLinePath.reset()
            mLinePath.moveTo(this.width.toFloat() * 0.5f, 0.0f)
            mLinePath.lineTo(this.width.toFloat() * 0.5f, this.height.toFloat())
            canvas.drawPath(mLinePath, mPaint!!)
        } else if (mDashedType == 2) {
            val radius = Math.min(this.width, this.height).toFloat() * 0.5f - mStrokeWidth * 0.5f
            canvas.drawCircle(
                this.width.toFloat() * 0.5f, this.height.toFloat() * 0.5f, radius,
                mPaint!!
            )
        }
    }

    fun setDashColor(color: Int) {
        mDashColor = color
    }

    fun setStrokeWidth(width: Float) {
        if (width > 0.0f && mStrokeWidth != width) {
            mStrokeWidth = width
        }
    }

    fun setGapAndWidth(gap: Int, width: Int) {
        if (width > 0 && width != mDashWidth) {
            mDashWidth = width
        }
        if (gap > 0 && mDashGap != gap) {
            mDashGap = gap
        }
    }

    annotation class DashedType {
        companion object {
            var HORIZONTAL_DASHED = 0
            var VERTICAL_DASHED = 1
            var CIRCLE_DASHED = 2
        }
    }
}
